import json
import os
import logging
import time
import threading
import requests
from flask import Flask, request
from flask_cors import CORS
from util.common_utils import resp_json
from util import path, msg_manage, get_repo_info
from util import get_single_package_source_info
from util.server_api.master import AgentManage
import session_manage
from server_rpm_agent_config import config

# msg_man = msg_manage.MsgQueue()
am = AgentManage()
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(message)s', datefmt='%Y/%m/%d %H:%M:%S')
app = Flask(__name__)


@app.route('/get_info4agent', methods=['get', 'post'])
def get_info4agent():
    return resp_json(msg="get_info4agent", data=am.get_agent_info_all())


@app.route('/agent_register', methods=['get', 'post'])
def agent_register():
    agent_link = request.json.get('agent_link', '')
    # print(agent_link)
    # agent_link = path.get_time_stamp_random()
    agent_key = am.register_by_link(agent_link)
    # am.agent_heart_check()
    return resp_json(msg="agent_register_success", data=am.get_agent_info_one(agent_key))


CORS(app, resources=r'/*')
if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=9999, threaded=True)  # 运行app
